/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import entity.User;
import java.util.Map;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
  
@FacesConverter("userConverter")
public class UserConverter implements Converter {
 
    public Object getAsObject( FacesContext fc , UIComponent uic , String value ) 
    {
        if( value != null && value.trim().length() > 0 ) 
        {
            try 
            {
                Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
                CreateEventBean createEventBean = ( CreateEventBean )viewMap.get( "createEventBean" );
                for( User u : createEventBean.getUserList() )
                    if( u.getNickname().equals( value ))
                        return u;
                return null;
            } 
            catch( NumberFormatException e )
            {
                System.out.println( "errore conversione utente" );
                return null;
            }
        }
        else 
        {
            return null;
        }
    }
 
    public String getAsString( FacesContext fc , UIComponent uic , Object object ) 
    {
        if( object != null ) 
        {
            return String.valueOf((( User )object ).getNickname() );
        }
        else 
        {
            return null;
        }
    }  
}    
